import axios from 'axios'; 

let token=''
if(localStorage.getItem('token')){
    token = localStorage.getItem('token');
}else{
    token = '';
}
axios.defaults.headers.common['Authorization'] = 'Bearer ' + token;
//设置地址和 token
axios.defaults.baseURL = 'http://veykoo.com:7777';

axios.interceptors.response.use(function(res) {
    let token=''
    if(localStorage.getItem('token')){
        token = localStorage.getItem('token');
    }else{
        token = '';
    }
    axios.defaults.headers.common['Authorization'] = 'Bearer ' + token;
    if(res.data.ret < 0){
        alert(res.data.msg);
        return res.data;
    }

    /// TODO why? ask who?
    /// login seems not work.
    if(res.data.ret === 999){
        window.location.hash = '#/login';
        return;
    }
    return res.data;
}, function (error) {
    console.log("response error:" + error);
    if(error == 'Error: Request failed with status code 401') {
        console.log("401 (Unauthorized)");
        // window.location.href = '#/glogin';
        //location.hash = '#/glogin';
    }
    return Promise.reject(error);
});

export default axios;